#global !p
#texMathZones = ['texMathZone' + x for x in ['A', 'AS', 'B', 'BS', 'C', 'CS', 'D', 'DS', 'E', 'ES', 'F', 'FS', 'G', 'GS', 'H', 'HS', 'I', 'IS', 'J', 'JS', 'K', 'KS', 'L', 'LS', 'DS', 'V', 'W', 'X', 'Y', 'Z', 'AmsA', 'AmsB', 'AmsC', 'AmsD', 'AmsE', 'AmsF', 'AmsG', 'AmsAS', 'AmsBS', 'AmsCS', 'AmsDS', 'AmsES', 'AmsFS', 'AmsGS' ]]
#texIgnoreMathZones = ['texMathText']
#texMathZoneIds = vim.eval('map('+str(texMathZones)+", 'hlID(v:val)')")
#texIgnoreMathZoneIds = vim.eval('map('+str(texIgnoreMathZones)+", 'hlID(v:val)')")
#ignore = texIgnoreMathZoneIds[0]
#def math():
	#synstackids = vim.eval("synstack(line('.'), col('.') - (col('.')>=2 ? 1 : 0))")
	#try:
		#first = next(i for i in reversed(synstackids) if i in texIgnoreMathZoneIds or i in texMathZoneIds)
		#return first != ignore
	#except StopIteration:
		#return False
#endglobal


#context "math()"
#snippet '([A-Za-z])(\d)' "auto subscript(c-e)" wrA
#`!p snip.rv = match.group(1)`_`!p snip.rv = match.group(2)`
#endsnippet

#snippet '([A-Za-z])_(\d\d)' "auto subscript2(c-e)" wrA
#`!p snip.rv = match.group(1)`_{`!p snip.rv = match.group(2)`}
#endsnippet


#context "math()"
#snippet // "Fraction(c-e)" iA
#\\frac{$1}{$2}$0
#endsnippet

#snippet / "Fraction(c-e)" i
#\\frac{${VISUAL}}{$1}$0
#endsnippet

#context "math()"
#snippet '((\d+)|(\d*)(\\)?([A-Za-z]+)((\^|_)(\{\d+\}|\d))*)/' "symbol frac(c-e)" wrA
#\\frac{`!p snip.rv = match.group(1)`}{$1}$0
#endsnippet

#context "math()"
#snippet ds "displaystyle(c-e)" i
#\\displaystyle $0
#endsnippet

#context "math()"
#snippet sum "\sum(c-e)" w
#\\sum_{$1}^{$2}$0
#endsnippet

#context "math()"
#snippet sqrt "\sqrt(c-e)" A
#\\sqrt{$1}$0
#endsnippet

#context "math()"
#snippet || "||(c-e)" A
#\\left\|{$1}\right\|$0
#endsnippet

#priority 10
#context "math()"
#snippet "bar" "bar(c-e)" iA
#\overline{$1}$0
#endsnippet

#priority 100
#context "math()"
#snippet "([a-zA-Z])bar" "bar(c-e)" riA
#\overline{`!p snip.rv=match.group(1)`}
#endsnippet

#priority 10
#context "math()"
#snippet "vec" "vec(c-e)"  iA
#\\vec{$1}$0
#endsnippet

#priority 100
#context "math()"
#snippet "([a-zA-Z])vec" "vec(c-e)" riA
#\\vec{`!p snip.rv=match.group(1)`}
#endsnippet

#snippet doc "\documentclass{}(c-e)" wb
#\documentclass{$1}
#$2
#\begin{document}
  #$0
#\end{document}
#endsnippet

#snippet docu "\documentclass[]{}(c-e)" wb
#\documentclass[$1]{$2}
#$3
#\begin{document}
  #$0
#\end{document}
#endsnippet

#snippet pac "\usepackage{}(c-e)" wb
#\usepackage{$1}
#endsnippet

#snippet pack "\usepackage[]{}(c-e)" wb
#\usepackage[$1]{$2}
#endsnippet

#snippet pages "pagestyle" bA
#\pagestyle{${1:plain}}
#endsnippet

#snippet beg "\begin{}...\end{}(c-e)" wb
#\begin{$1}
  #$2
#\end{$1}
#endsnippet

#snippet math "\[...\](c-e)" wb
#\[
  #${1}
#\]
#endsnippet

#priority 200
#context "math()"
#snippet '(?<!\\)(arcsin|arccos|arctan|arccot|arccsc|arcsec|pi|zeta|int)' "ln" rwA
#\\`!p snip.rv = match.group(1)`
#endsnippet


#priority 100
#context "math()"
#snippet -> "to" iA
#\to 
#endsnippet

#priority 200
#context "math()"
#snippet <-> "leftrightarrow" iA
#\leftrightarrow
#endsnippet


global !p
texMathZones = ['texMathZone' + x for x in ['A', 'AS', 'B', 'BS', 'C', 'CS', 'D', 'DS', 'E', 'ES', 'F', 'FS', 'G', 'GS', 'H', 'HS', 'I', 'IS', 'J', 'JS', 'K', 'KS', 'L', 'LS', 'DS', 'V', 'W', 'X', 'Y', 'Z', 'AmsA', 'AmsB', 'AmsC', 'AmsD', 'AmsE', 'AmsF', 'AmsG', 'AmsAS', 'AmsBS', 'AmsCS', 'AmsDS', 'AmsES', 'AmsFS', 'AmsGS' ]]
texIgnoreMathZones = ['texMathText']
texMathZoneIds = vim.eval('map('+str(texMathZones)+", 'hlID(v:val)')")
texIgnoreMathZoneIds = vim.eval('map('+str(texIgnoreMathZones)+", 'hlID(v:val)')")
ignore = texIgnoreMathZoneIds[0]
def math():
	synstackids = vim.eval("synstack(line('.'), col('.') - (col('.')>=2 ? 1 : 0))")
	try:
		first = next(i for i in reversed(synstackids) if i in texIgnoreMathZoneIds or i in texMathZoneIds)
		return first != ignore
	except StopIteration:
		return False
endglobal


context "math()"
snippet '([A-Za-z])(\d)' "auto subscript" wrA
`!p snip.rv = match.group(1)`_`!p snip.rv = match.group(2)`
endsnippet

snippet '([A-Za-z])_(\d\d)' "auto subscript2" wrA
`!p snip.rv = match.group(1)`_{`!p snip.rv = match.group(2)`}
endsnippet


context "math()"
snippet // "Fraction" iA
\\frac{$1}{$2}$0
endsnippet

snippet / "Fraction" i
\\frac{${VISUAL}}{$1}$0
endsnippet

context "math()"
snippet '((\d+)|(\d*)(\\)?([A-Za-z]+)((\^|_)(\{\d+\}|\d))*)/' "symbol frac" wrA
\\frac{`!p snip.rv = match.group(1)`}{$1}$0
endsnippet

context "math()"
snippet ds "displaystyle" i
\\displaystyle $0
endsnippet

context "math()"
snippet sum "\sum" w
\\sum_{$1}^{$2}$0
endsnippet

context "math()"
snippet sqrt "\sqrt" wA
\\sqrt{$1}$0
endsnippet

context "math()"
snippet || "||" A
\\left\|{$1}\right\|$0
endsnippet

priority 10
context "math()"
snippet "([^\\])bar" "bar" wiA
\overline{${1:${VISUAL}}}$0
endsnippet

priority 100
context "math()"
snippet "([a-zA-Z])bar" "bar" riA
\overline{`!p snip.rv=match.group(1)`}
endsnippet

priority 10
context "math()"
snippet "([^\\])vec" "vec"  riA
\\vec{${1:${VISUAL}}}$0
endsnippet

priority 100
context "math()"
snippet "([a-zA-Z])vec" "vec" riA
\\vec{`!p snip.rv=match.group(1)`}
endsnippet

priority 10
context "math()"
snippet "([^\\])bm" "bm"  riA
\\bm{${1:${VISUAL}}}$0
endsnippet

priority 100
context "math()"
snippet "([a-zA-Z])bm" "bm" riA
\\bm{`!p snip.rv=match.group(1)`}
endsnippet

priority 10
context "math()"
snippet "([^\\])bf" "textbf"  riA
{\\textbf{${1:${VISUAL}}}}$0
endsnippet

priority 100
context "math()"
snippet "([a-zA-Z])bf" "textbf" riA
{\\textbf{`!p snip.rv=match.group(1)`}}
endsnippet

snippet doc "\documentclass{}" wb
\documentclass{$1}
$2
\begin{document}
  $0
\end{document}
endsnippet

snippet docu "\documentclass[]{}" wb
\documentclass[$1]{$2}
$3
\begin{document}
  $0
\end{document}
endsnippet

snippet pac "\usepackage{}" wb
\usepackage{$1}
endsnippet

snippet pack "\usepackage[]{}" wb
\usepackage[$1]{$2}
endsnippet

snippet pages "pagestyle" bA
\pagestyle{${1:plain}}
endsnippet

snippet beg "\begin{}...\end{}" wb
\begin{$1}
  $2
\end{$1}
endsnippet

snippet dm "\[...\]" wbA
\[
  ${1:${VISUAL}}
\]
endsnippet


context "math()"
snippet rl) "left( right)" iA
\left( ${1:${VISUAL}} \right) $0
endsnippet

context "math()"
snippet rl| "left| right|" iA
\left| ${1:${VISUAL}} \right| $0
endsnippet

context "math()"
snippet rl} "left{ right}" iA
\left\\{ ${1:${VISUAL}} \right\\} $0
endsnippet

snippet ooo "\infty" iA
\infty
endsnippet

snippet >> ">>" iA
\gg
endsnippet

snippet << "<<" iA
\ll
endsnippet

snippet ~~ "~" iA
\sim 
endsnippet

context "math()"
snippet !> "mapsto" iA
\mapsto 
endsnippet

priority 100
context "math()"
snippet '(?<!\\)(sin|cos|tan|cot|csc|sec|Omega|alpha|beta|omega)' "ln" rwA
\\`!p snip.rv=match.group(1)`
endsnippet

priority 200
context "math()"
snippet '(?<!\\)(arcsin|arccos|arctan|arccot|arccsc|arcsec|pi|zeta|int)' "ln" rwA
\\`!p snip.rv=match.group(1)`
endsnippet

priority 300
context "math()"
snippet '(?<!\\)(iint|oint)' "ln" rwA
\\`!p snip.rv=match.group(1)`
endsnippet

priority 400
context "math()"
snippet '(?<!\\)(iiint|oiint)' "ln" rwA
\\`!p snip.rv=match.group(1)`
endsnippet

priority 500
context "math()"
snippet '(?<!\\)(iiiint|oiiint)' "ln" rwA
\\`!p snip.rv=match.group(1)`
endsnippet

priority 100
context "math()"
snippet -> "to" iA
\to 
endsnippet

priority 200
context "math()"
snippet <-> "leftrightarrow" iA
\leftrightarrow
endsnippet

snippet != "equals" iA
\neq 
endsnippet

context "math()"
snippet ceil "ceil" iA
\left\lceil $1 \right\rceil $0
endsnippet

context "math()"
snippet sr "^2" iA
^2
endsnippet

context "math()"
snippet "([\w})\]])\.t" "transpose" irA
`!p snip.rv=match.group(1)`^\top
endsnippet

context "math()"
snippet cb "^3" iA
^3
endsnippet

context "math()"
snippet tp "to the ... power" iA
^{$1}$0
endsnippet

context "math()"
snippet "ts" "subscript" riA
_{$1}$0
endsnippet

context "math()"
snippet xx "cross" iA
\times 
endsnippet

context "math()"
snippet ** "cdot" iA
\cdot 
endsnippet

context "math()"
snippet part "dy/dx" w
\frac{\partial ${1:V}}{\partial ${2:x}} $0
endsnippet
